package com.czl.module_storehouse.activity.managegoods.add;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.czl.base.data.bean.tengyun.CategoryBean;
import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.base.util.GlideEngine;
import com.czl.base.util.SpHelper;
import com.czl.module_base.activity.BaseActivity;
import com.czl.module_base.bean.CompanyBean;
import com.czl.module_base.bean.FileServiceResult;
import com.czl.module_base.http.HttpResponse;
import com.czl.module_base.mvp.presenter.InjectPresenter;
import com.czl.module_base.mvp.view.SimpleView;
import com.czl.module_base.utils.GlideUtils;
import com.czl.module_base.utils.ParcelHelper;
import com.czl.module_base.utils.PopWindowUtils;
import com.czl.module_service.system.Constants;
import com.czl.module_service.view.ListSheetDialogFragment;
import com.czl.module_storehouse.R;
import com.czl.module_base.adapter.CategoryAdapter;
import com.czl.module_base.adapter.ListTree;
import com.czl.module_base.adapter.ListTreeAdapter;
import com.czl.module_storehouse.bean.SortInfoBean;
import com.czl.module_storehouse.databinding.ActivityGoodsAddBinding;
import com.czl.module_storehouse.event.AddWareSortEvent;
import com.czl.module_storehouse.event.AllocateSortEvent;
import com.czl.module_storehouse.event.DirectAddGoodsEvent;
import com.czl.module_storehouse.event.SortEvent;
import com.czl.module_storehouse.mvp.presenter.CategoryPresenter;
import com.czl.module_storehouse.mvp.presenter.CompanyPresenter;
import com.czl.module_storehouse.mvp.presenter.SortPresenter;
import com.czl.module_storehouse.mvp.presenter.UploadPresenter;
import com.czl.module_storehouse.mvp.view.UploadView;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.listener.OnResultCallbackListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class GoodsAddActivity extends BaseActivity<ActivityGoodsAddBinding> implements SimpleView, UploadView {

    private int mEventTag;
    private SortBean mSortBean = new SortBean();

    private List<CompanyBean> mCompanyBeanList;
    private List<CategoryBean> mCategoryBeanList;
    @InjectPresenter
    SortPresenter mSortPresenter;

    @InjectPresenter
    CategoryPresenter mCategoryPresenter;

    @InjectPresenter
    CompanyPresenter mCompanyPresenter;

    @InjectPresenter
    UploadPresenter mUploadPresenter;
    /**
     * 0 固定资产
     */
    private Integer mAssetType;

    @Override
    protected ActivityGoodsAddBinding getBinding(LayoutInflater layoutInflater) {
        return ActivityGoodsAddBinding.inflate(layoutInflater);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mToolBinding.toolbarContentTitle.setText("添加物品");
        EventBus.getDefault().register(this);
        if (mSortBean != null && mSortBean.getEditable() != null && mSortBean.getEditable()) {
            mToolBinding.toolbarContentTitle.setText("编辑物品");
            binding.clCode.setVisibility(Constants.SortBean.MANAGE_MODE_PRODUCT == mSortBean.manageMode() ? View.VISIBLE : View.GONE);
        }
        CompanyBean bean = new CompanyBean();
        if (mSortBean != null) {
            Integer propCompanyId = mSortBean.getPropCompanyId();
            if (propCompanyId != null) {
                bean.setCompanyId(String.valueOf(mSortBean.getPropCompanyId()));
            }
        } else {
            bean.setCompanyId(String.valueOf(SpHelper.INSTANCE.decodeInt(Constants.SpKey.MANAGE_COMPANY_ID)));
        }
        if (mSortBean != null && !mSortBean.editable()) {
            //添加物品
            mSortBean.setPropCompanyId(SpHelper.INSTANCE.decodeInt(Constants.SpKey.MANAGE_COMPANY_ID));
            mSortBean.setPropCompanyName(SpHelper.INSTANCE.decodeString(Constants.SpKey.MANAGE_COMPANY_NAME));
            mSortBean.setPropCompanyShortName(SpHelper.INSTANCE.decodeString(Constants.SpKey.MANAGE_COMPANY_SHORT_NAME));
        }
        bean.setCompanyName(SpHelper.INSTANCE.decodeString(Constants.SpKey.MANAGE_COMPANY_NAME));
        bean.setCompanyShortName(SpHelper.INSTANCE.decodeString(Constants.SpKey.MANAGE_COMPANY_SHORT_NAME));
        if (mCompanyBeanList == null) {
            mCompanyBeanList = new ArrayList<>();
        }
        mCompanyBeanList.add(bean);
        binding.tvCompany.setText(SpHelper.INSTANCE.decodeString(Constants.SpKey.MANAGE_COMPANY_SHORT_NAME));
        initListener();
    }

    private void initListener() {
        //选择产权公司
        binding.tvCompany.setOnClickListener(v -> selectCompanyList());
        //选择分类
        binding.tvSort.setOnClickListener(v -> getClassificationListCompany(null));
        //选择管理方式
        binding.tvManageMode.setOnClickListener(v -> showManageModePopup());
        //选择使用方式
        binding.tvUseMode.setOnClickListener(v -> showUseModeModePopup());
        //选择计价方式
        binding.tvValuationMode.setOnClickListener(v -> showValuationModeModePopup());
        //上传物品图片
        binding.ivAddImage.setOnClickListener(v -> showChoosePhoto());
        //是否贴二维码
        binding.tvHasCode.setOnClickListener(v -> showHasCodePopup());
        //提交
        binding.textCommit.setOnClickListener(v -> submit());
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(DirectAddGoodsEvent event) {
        binding.tvCompany.setText(SpHelper.INSTANCE.decodeString(Constants.SpKey.MANAGE_COMPANY_SHORT_NAME));
        mSortBean.setPropCompanyId(SpHelper.INSTANCE.decodeInt(Constants.SpKey.MANAGE_COMPANY_ID));
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(SortEvent event) {
        SortBean sortBean = event.getSortBean();
        if (sortBean != null) {
            mSortBean = sortBean;
        }
        setupView(mSortBean);
    }

    /**
     * 入库没有物品创建物品
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(AddWareSortEvent event) {
        if (event == null) {
            return;
        }
        mAssetType = event.getAssetType();
        SortBean sortBean = event.getSortBean();
        if (sortBean != null) {
            mEventTag = 1;
            mSortBean = ParcelHelper.copy(sortBean);
            setupView(mSortBean);
            binding.tvSort.setText("请选择分类");
            binding.etName.setEnabled(false);
            binding.etModel.setEnabled(false);
            binding.tvCompany.setEnabled(false);
            binding.tvCompany.setCompoundDrawables(null, null, null, null);
            Integer sortPid = sortBean.getSortPId();
            if (sortPid == null || 0 == sortPid) {
                if (mCompanyPresenter != null) {
                    mCompanyPresenter.selectByCompanyId(sortBean.getPropCompanyId());
                }
            } else {
                selectCompanyAndSort(mSortBean);
            }
        }
    }

    /**
     * 调拨没有物品创建物品
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(AllocateSortEvent event) {
        SortBean sortBean = event.getSortBean();
        if (sortBean != null) {
            mSortBean = sortBean;
            mSortBean.setSortId(0);
            binding.tvProCompany.setVisibility(View.VISIBLE);
            binding.tvProCompany.setText(sortBean.getPropCompanyShortName());
            getClassificationListCompany(sortBean.getPIdPath());
            binding.tvSortName.setVisibility(View.VISIBLE);
            binding.tvSort.setVisibility(View.GONE);
            binding.tvCompany.setVisibility(View.GONE);
            setupView(sortBean);
            binding.etName.setEnabled(false);
            binding.etModel.setEnabled(false);
        }
    }

    private void setupView(SortBean sortBean) {
        if (sortBean == null) {
            return;
        }
        binding.tvCompany.setText(sortBean.getPropCompanyShortName());
        binding.tvSort.setText(sortBean.getClassificationName() == null ? "请选择分类" : sortBean.getClassificationName());
        binding.etName.setText(sortBean.getSortName());
        binding.etCode.setText(sortBean.getSortCode());
        binding.etModel.setText(sortBean.getSortModel());
        binding.etUnit.setText(sortBean.getUnit());
        binding.tvManageMode.setText(sortBean.getManageModeStr() == null ? "选择管理方式" : sortBean.getManageModeStr());
        binding.tvUseMode.setText(sortBean.getUseModeStr() == null ? "选择使用方式" : sortBean.getUseModeStr());
        binding.tvValuationMode.setText(sortBean.getValuationStr() == null ? "选择计价方式" : sortBean.getValuationStr());
        binding.tvHasCode.setText(sortBean.pasteCode() ? "是" : "否");
        binding.clCode.setVisibility(Constants.SortBean.MANAGE_MODE_PRODUCT == mSortBean.manageMode() ? View.VISIBLE : View.GONE);
        if (sortBean.editable()) {
            binding.tvCompany.setCompoundDrawables(null, null, null, null);
            binding.tvSort.setCompoundDrawables(null, null, null, null);
            binding.etCode.setCompoundDrawables(null, null, null, null);
            binding.tvManageMode.setCompoundDrawables(null, null, null, null);
            binding.tvHasCode.setCompoundDrawables(null, null, null, null);
            //可以更改
            binding.tvHasCode.setEnabled(true);
            binding.tvCompany.setEnabled(false);
            binding.tvSort.setEnabled(false);
            binding.etCode.setEnabled(false);
            binding.tvManageMode.setEnabled(false);
            getClassificationListCompany(null);
        }

        if (!TextUtils.isEmpty(sortBean.getSortImg())) {
            GlideUtils.loadImage(getContext(), binding.ivAddImage, R.mipmap.pic_default_loading, sortBean.getSortImg());
        }
    }

    private void submit() {
        Integer propCompanyId = mSortBean.getPropCompanyId();
        if (propCompanyId == null || propCompanyId == 0) {
            showToast("请选择产权公司");
            return;
        }
        Integer sortPid = mSortBean.getSortPId();
        if (sortPid == null || sortPid == 0) {
            showToast("请选择所属分类");
            return;
        }
        String sortName = binding.etName.getText().toString();
        if (TextUtils.isEmpty(sortName)) {
            showToast("请输入物品名称");
            return;
        }
        mSortBean.setSortName(sortName);
        String sortCode = binding.etCode.getText().toString();
        mSortBean.setSortCode(sortCode);
        String sortModel = binding.etModel.getText().toString();
        mSortBean.setSortModel(sortModel);
        String unit = binding.etUnit.getText().toString();
        mSortBean.setUnit(unit);
        Integer manageMode = mSortBean.getManageMode();
        if (manageMode == null || manageMode < 1) {
            showToast("请选择管理方式");
            return;
        }
        Integer useMode = mSortBean.getUseMode();
        if (useMode == null || useMode < 1) {
            showToast("请选择使用方式");
            return;
        }
        Integer valuation = mSortBean.getValuation();
        if (valuation == null) {
            showToast("请选择计价方式");
            return;
        }
//        String sortCode = mSortInfoBean.getSortCode();
//        if (TextUtils.isEmpty(sortCode)) {
//            showToast("请输入物品编号");
//            return;
//        }
//        String unit = mSortInfoBean.getUnit();
//        if (TextUtils.isEmpty(unit)) {
//            showToast("请输入物品单位");
//            return;
//        }
//        setCommitView(textCommit);

        if (mSortPresenter != null) {
            if (mSortBean.editable()) {
                mSortPresenter.editSortInfo(mSortBean);
            } else {
                mSortPresenter.addSortInfo(mSortBean);
            }
        }
    }

    private void selectCompanyList() {
        if (mCompanyBeanList == null) {
            if (mCompanyPresenter != null) {
                mCompanyPresenter.selectCompanyList();
            }
        } else {
            showCompanyPopup();
        }
    }

    private void selectCompanyAndSort(SortBean sortBean) {
       /* if (mCompanyPresenter != null) {
            mCompanyPresenter.selectCompanyAndSort(sortBean);
        }*/
    }

    private void selectCompanyId(SortBean sortBean) {
        if (mCompanyPresenter != null) {
//            mCompanyPresenter.selectCompanyAndSort(sortBean);
        }
    }

    /**
     * 选择分类 (设施0、2) 分类1
     *
     * @param pIdPath
     */
    private void getClassificationListCompany(String pIdPath) {
        Integer propCompanyId = mSortBean.getPropCompanyId();
        if (propCompanyId == null || 0 == propCompanyId) {
            showToast("请选择产权公司");
            return;
        }
        List<Integer> classIds = mSortBean.getClassIds(mSortBean.getAddSortType() != null
                && Constants.SortBean.ADD_TYPE_SORT == mSortBean.getAddSortType());
        if (mAssetType != null && mAssetType == 0) {
            //采购入库，固定资产查询分类
            if (mCategoryPresenter != null) {
                mCategoryPresenter.getclassificationlistCompany(propCompanyId, mSortBean, Arrays.asList(0, 2));
            }
        } else {
            if (mSortBean.editable()) {
                if (mCategoryPresenter != null) {
                    mCategoryPresenter.getclassificationlistCompany(propCompanyId, mSortBean, classIds);
                }
            } else {
                if (mCategoryPresenter != null) {
                    mCategoryPresenter.getclassificationlistCompany(String.valueOf(propCompanyId));
                }
            }
        }
    }

    /**
     * 选择公司
     */
    private void showCompanyPopup() {
        List<String> list = new ArrayList<>();
        if (mCompanyBeanList != null) {
            for (CompanyBean bean : mCompanyBeanList) {
                String companyName = bean.getCompanyShortName();
                if (TextUtils.isEmpty(companyName)) {
                    continue;
                }
                list.add(companyName);
            }
        }
        showRecyclerViewPopupWindow(list, "选择公司", (adapter, view, position) -> {
            CompanyBean bean = mCompanyBeanList.get(position);
            if (!TextUtils.isEmpty(bean.getCompanyId())) {
                mSortBean.setPropCompanyId(Integer.parseInt(bean.getCompanyId()));
            }
            mSortBean.setPropCompanyName(bean.getCompanyName());
            mSortBean.setPropCompanyShortName(bean.getCompanyShortName());
            binding.tvCompany.setText(bean.getCompanyShortName());
        });
    }

    /**
     * 分类弹框
     */
    private void showSortPopup() {
        if (mCategoryBeanList == null || mCategoryBeanList.size() == 0) {
            showToast("没有数据");
            return;
        }
        ListTree listTree = new ListTree();
        View view = View.inflate(getContext(), R.layout.common_recycler_view, null);
        RecyclerView recyclerView = view.findViewById(R.id.rv_item);
        recyclerView.setPadding(0, 0, 0, SizeUtils.dp2px(20));
        CategoryAdapter categoryAdapter = new CategoryAdapter(listTree);
        categoryAdapter.setOnlySelectedLastLevel(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(categoryAdapter);
        categoryAdapter.addAll(mCategoryBeanList);
        int itemCount = categoryAdapter.getItemCount();
        int width = ScreenUtils.getScreenWidth() - SizeUtils.dp2px(30);
        if (itemCount > 8) {
            int height = (int) (ScreenUtils.getScreenHeight() * 0.6f);
            PopWindowUtils.getInstance().createPopWindow(getContext(), view, width, height, "选择分类");
        } else {
            PopWindowUtils.getInstance().createPopWindow(getContext(), view, width, "选择分类");
        }
        PopWindowUtils.getInstance().showBasePopupWindow(binding.getRoot());
        categoryAdapter.setOnItemClickListener((ListTreeAdapter.OnItemClickListener<CategoryBean>) (categoryBean, title, pIdPath) -> {
            PopWindowUtils.getInstance().dismissPopupWindow();

            mSortBean.setSortPId(categoryBean.getId());
            mSortBean.setSortName(categoryBean.getParentName());
            mSortBean.setClassStatus(categoryBean.getClassStatus());

            mSortBean.setPidPath(categoryBean.getParentAbsoluteId());
            mSortBean.setSortType(categoryBean.getClassStatus());
            mSortBean.setClassificationName(categoryBean.getName());
//            mLastSortId = categoryBean.getId() == null ? 0 : categoryBean.getId();
            binding.tvSort.setText(categoryBean.getParentName());
            resetSort();
            getSortCode(String.valueOf(categoryBean.getId()), categoryBean.getClassificationCode());
        });
    }

    private void getSortCode(String sortPid, String pCode) {
        if (mSortPresenter != null) {
            mSortPresenter.getSortCode(sortPid, pCode);
        }
    }

    private void resetSort() {
        binding.tvManageMode.setText("选择管理方式");
        binding.tvUseMode.setText("选择使用方式");
        binding.tvValuationMode.setText("选择计价方式");
        mSortBean.setManageMode(0);
        mSortBean.setUseMode(0);
        mSortBean.setValuation(null);
    }

    private void resetUseMode() {
        binding.tvUseMode.setText("选择使用方式");
        binding.tvValuationMode.setText("选择计价方式");
        mSortBean.setUseMode(0);
        mSortBean.setValuation(null);
    }

    /**
     * 管理方式弹框
     */
    private void showManageModePopup() {
        Integer pId = mSortBean.getSortPId();
        if (pId == null || "请选择分类".equals(binding.tvSort.getText().toString())) {
            showToast("请选择所属分类");
            return;
        }
        //1.单品，2.数量
        List<String> list = new ArrayList<>();
        Integer classStatus = mSortBean.getClassStatus();
        if (classStatus != null && SortInfoBean.TAG_PRODUCT == classStatus) {
            list.add("单品");
        } else {
            list.add("单品");
            list.add("数量");
        }
        showRecyclerViewPopupWindow(list, "管理方式", (adapter, view, position) -> {
            if (position == 0) {
                mSortBean.setPasteCode(true);
                binding.tvHasCode.setText("是");
            }
            mSortBean.setManageMode(position + 1);
            String name = list.get(position);
            mSortBean.setManageModeStr(name);
            binding.tvManageMode.setText(name);
            resetUseMode();
            binding.clCode.setVisibility(Constants.SortBean.MANAGE_MODE_PRODUCT == mSortBean.manageMode() ? View.VISIBLE : View.GONE);
        });
    }

    /**
     * 使用方式弹框
     */
    private void showUseModeModePopup() {
        Integer pId = mSortBean.getSortPId();
        if (pId == null || "请选择分类".equals(binding.tvSort.getText().toString())) {
            showToast("请选择所属分类");
            return;
        }
        //使用方式：1.耐用品，2.消耗品
        List<String> list = new ArrayList<>();
        Integer manageMode = mSortBean.getManageMode();
        list.add("耐用品");
        if (manageMode != null && manageMode != 1) {
            list.add("消耗品");
        }
        showRecyclerViewPopupWindow(list, "使用方式", (adapter, view, position) -> {
            mSortBean.setUseMode(position + 1);
            String name = list.get(position);
            mSortBean.setUseModeStr(name);
            binding.tvUseMode.setText(name);
        });
    }

    /**
     * 是否贴码
     */
    private void showHasCodePopup() {
        Integer sortpid = mSortBean.getSortPId();
        if (sortpid == null || "请选择分类".equals(binding.tvSort.getText().toString())) {
            showToast("请选择所属分类");
            return;
        }
        Integer manageMode = mSortBean.getManageMode();
        if (manageMode == null || manageMode == 0) {
            showToast("请选择管理方式");
            return;
        }
        List<String> list = new ArrayList<>();
        if (manageMode == Constants.SortBean.MANAGE_MODE_PRODUCT) {
            list.add("是");
            list.add("否");
        }
        showRecyclerViewPopupWindow(list, "是否贴码", (adapter, view, position) -> {
            binding.tvHasCode.setText(list.get(position));
            mSortBean.setPasteCode(position == 0);
        });
    }

    /**
     * 计价方式弹框
     */
    private void showValuationModeModePopup() {
        Integer sortPId = mSortBean.getSortPId();
        if (sortPId == null || "请选择分类".equals(binding.tvSort.getText().toString())) {
            showToast("请选择所属分类");
            return;
        }
        //计价方式：0.个别计价法，1.移动加权平均法，2.先进先出法
        List<String> list = new ArrayList<>();
        Integer classStatus = mSortBean.getClassStatus();
        Integer manageMode = mSortBean.getManageMode();
        if (classStatus == null || manageMode == null) {
            return;
        }
        if (SortInfoBean.TAG_PRODUCT == classStatus || 1 == manageMode) {
            list.add("个别计价法");
        } else {
            list.add("移动加权平均法");
            list.add("先进先出法");
        }
        showRecyclerViewPopupWindow(list, "计价方式", (adapter, view, position) -> {
            if (SortInfoBean.TAG_PRODUCT == classStatus || 1 == manageMode) {
                mSortBean.setValuation(position);
            } else {
                mSortBean.setValuation(position + 1);
            }
            String name = list.get(position);
            mSortBean.setValuationStr(name);
            binding.tvValuationMode.setText(name);
        });
    }

    private void showChoosePhoto() {
        ListSheetDialogFragment.Companion.showBottomSheet(getSupportFragmentManager(), Arrays.asList("拍照", "相册"), integer -> {
            if (integer == null) {
                return null;
            }
            if (integer == 1) {
                PictureSelector.create(GoodsAddActivity.this)
                        .openGallery(PictureMimeType.ofImage())
                        .imageEngine(GlideEngine.createGlideEngine())
                        .videoMaxSecond(15)
                        .isShowOutLengthVideos(true)
                        .isMaxSelectEnabledMask(true)
                        .maxSelectNum(1)
                        .maxVideoSelectNum(4)
                        .imageSpanCount(4)
                        .isWithVideoImage(true)
                        .selectionMode(PictureConfig.SINGLE)
                        .isCamera(false)
                        .isEditorImage(true)
                        .selectionData(new ArrayList<>())
                        .isCompress(true)
                        .forResult(new OnResultCallbackListener<LocalMedia>() {
                            @Override
                            public void onResult(List<LocalMedia> list) {
                                uploadFile(list);
                            }

                            @Override
                            public void onCancel() {

                            }
                        });
            } else {
                PictureSelector.create(GoodsAddActivity.this)
                        .openCamera(PictureMimeType.ofImage())
                        .recordVideoSecond(15)
                        .imageEngine(GlideEngine.createGlideEngine())
                        .isUseCustomCamera(true)
                        .isCompress(true)
                        .forResult(new OnResultCallbackListener<LocalMedia>() {
                            @Override
                            public void onResult(List<LocalMedia> list) {
                                uploadFile(list);
                            }

                            @Override
                            public void onCancel() {

                            }
                        });
            }
            return null;
        });
    }

    private void uploadFile(List<LocalMedia> list) {
        if (!list.isEmpty()) {
            String fileName = list.get(0).getCompressPath();
            if (mUploadPresenter != null) {
                mUploadPresenter.uploadFile(fileName);
            }
        }
    }

    @Override
    public <T> void showHttpResponse(HttpResponse<T> httpResponse) {
        super.showHttpResponse(httpResponse);
        if (CompanyPresenter.TAG_COMPANY_LIST.equals(httpResponse.getRequestTag())) {
            mCompanyBeanList = (List<CompanyBean>) httpResponse.getData();
            //获取公司
            showCompanyPopup();
        } else if (CategoryPresenter.TAG_CATEGORY_LIST.equals(httpResponse.getRequestTag())) {
            //分类
            if (mSortBean.editable()) {
                if (httpResponse.getData() == null) {
                    return;
                }
                List<CategoryBean> list = (List<CategoryBean>) httpResponse.getData();
                if (list == null || list.isEmpty()) {
                    return;
                }
                String parentName = list.get(0).getParentName();
                binding.tvSortName.setText(parentName);
            } else {
                mCategoryBeanList = (List<CategoryBean>) httpResponse.getData();
                showSortPopup();
            }
        } else if (SortPresenter.TAG_SORT_ADD.equals(httpResponse.getRequestTag())) {
            //提交成功
            responseBySubmit(httpResponse);
        } else if (SortPresenter.TAG_SORT_EDIT.equals(httpResponse.getRequestTag())) {
            //编辑成功
            showToast("修改成功");
            SortEvent sortEvent = new SortEvent(mSortBean);
            sortEvent.setEdit(true);
            EventBus.getDefault().post(sortEvent);
            finish();
        } else if (SortPresenter.TAG_SORT_CODE.equals(httpResponse.getRequestTag())) {
            if (httpResponse.getData() instanceof SortBean) {
                binding.etCode.setText(((SortBean) httpResponse.getData()).getSortCode());
            }
        } else if (UploadPresenter.TAG_UPLOAD_FILE.equals(httpResponse.getRequestTag())) {
            //上传图片
            FileServiceResult result = (FileServiceResult) httpResponse.getData();
            String imageUrl = result.getUrl();
            mSortBean.setSortImg(imageUrl);
            GlideUtils.loadImage(getContext(), binding.ivAddImage, R.mipmap.pic_default_loading, imageUrl);
        }
    }

    private <T> void responseBySubmit(HttpResponse<T> httpResponse) {
        Object data = httpResponse.getData();
        if (data != null) {
            if (data instanceof SortBean) {
                Integer sortId = ((SortBean) data).getSortId();
                mSortBean.setInSortId(sortId);
                mSortBean.setExistCompany(true);
                if (this.mEventTag == 1) {
                    mSortBean.setSortId(sortId);
                }
            }
        }
        showToast("提交成功");
        SortEvent sortEvent = new SortEvent(mSortBean);
        EventBus.getDefault().post(sortEvent);
        finish();
    }

}
